package com.flot.yazb.home.moudle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.flot.yazb.R;
import com.flot.yazb.base.BFragment;
import com.flot.yazb.base.adapter.recycleview.BRAdapter;
import com.flot.yazb.base.adapter.recycleview.RVAdapter;
import com.flot.yazb.base.adapter.recycleview.RvHolder;
import com.flot.yazb.base.model.BModel;
import com.flot.yazb.aop.stub.CodeStub;
import com.flot.yazb.model.fate.FateModel;
import com.flot.yazb.model.nearly.NearLyModel;
import com.flot.yazb.model.user.UserModel;
import com.flot.yazb.home.setting.UserDetailActivity;
import com.flot.yazb.widget.utils.GlideImageLoaderUtil;
import com.flot.yazb.widget.utils.ToastUtil;
import com.flot.yazb.widget.MRecycleView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class NearFragment extends BFragment {

    @BindView(R.id.mrv)
    MRecycleView mRecycleview;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.tvgetfriendsetting)
    TextView tvgetfriendsetting;
    private List<NearLyModel> dataList = new ArrayList<>();
    private BRAdapter<NearLyModel> rvAdapter;
    private View view;
    private UserModel argsModel;

    @Override
    protected View initRootView(LayoutInflater inflater) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_home_near, null);
        }
        ButterKnife.bind(this, view);
        ViewGroup myparent = (ViewGroup) view.getParent();
        if (getArguments() != null) {
            argsModel = (UserModel) getArguments().getSerializable("args");
        }
        if (myparent != null) {
            myparent.removeView(view);
        }
        return view;
    }

    //利用setArgues传递参数
    public static NearFragment newInstance(UserModel model) {
        NearFragment fragment = new NearFragment();
        Bundle args = new Bundle();
        args.putSerializable("args", model);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        initAdapter();
        getData();
    }

    private void getData() {
        showProgress();
        NearLyModel.getNearPeopleList(new BModel.Transit<NearLyModel[]>(getActivity()) {
            @Override
            public void onBorn(NearLyModel[] serializable, String code, String message) {
                dismissProgress();
                if (code.equals(CodeStub.B_OK)) {
                    dataList.clear();
                    dataList.addAll(Arrays.asList(serializable));
                    rvAdapter.notifyDataSetChanged();
                    if (dataList.size()==0){
                        getData();
                    }
                } else {
                    ToastUtil.showToast(getActivity(), message);
                }
            }
        });
    }

    private void initAdapter() {
//        mRecycleview.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mRecycleview.setRefreshProgressStyle(ProgressStyle.BallScaleMultiple);//刷新样式
        mRecycleview.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);//加载样式
        mRecycleview.setLoadingMoreEnabled(false);
        mRecycleview.setPullRefreshEnabled(false);
        mRecycleview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //refresh data here
                ToastUtil.showToast(getActivity(), "refresh");
                mRecycleview.refreshComplete();

            }

            @Override
            public void onLoadMore() {
                // load more data here
                mRecycleview.loadMoreComplete();
                ToastUtil.showToast(getActivity(), "onLoadMore");
            }
        });

        rvAdapter = new BRAdapter<NearLyModel>(getActivity(), R.layout.activity_nearlist_item, dataList) {

            @Override
            protected void convert(RvHolder holder, final NearLyModel model, int position) {
                GlideImageLoaderUtil.loadImage((ImageView) holder.getView(R.id.icon), model.thumb, R.mipmap.activity_splash_background);
                holder.setText(R.id.name, model.name);
                holder.setText(R.id.tvHit, model.getHit());
                holder.setText(R.id.age, model.age+"岁");
                holder.setText(R.id.address, model.province);
                holder.setText(R.id.distance, " 距离："+model.distance+"km");
                RecyclerView recy=holder.getView(R.id.rview);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                recy.setLayoutManager(linearLayoutManager);
                BRAdapter imgAdapter = new BRAdapter<String>(getContext(), R.layout.activity_img_near_item, model.pics) {
                    @Override
                    protected void convert(RvHolder holder, String s, int position) {
                        GlideImageLoaderUtil.loadImage((ImageView) holder.getView(R.id.pic), s);
                    }
                };
                recy.setAdapter(imgAdapter);
            }
        };

        rvAdapter.setOnItemClickListener(new RVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                FateModel par = new FateModel();
                NearLyModel model = dataList.get(position - 1);
                par.mid = model.mid;
                par.name = model.name;
                par.thumb = model.thumb;
                par.age = model.age;
                Intent intent = new Intent(getActivity(), UserDetailActivity.class);
                intent.putExtra("mid", par);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View v, int position) {
            }
        });

        mRecycleview.setAdapter(rvAdapter);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @OnClick(R.id.tvgetfriendsetting)
    public void onClick() {
        ToastUtil.showToast(getActivity(),"征友设置");
    }
}
